package Boot;


import common.Exception.Results;
import contorller.AdminController;
import dto.req.AdminRegisterReqDTO;
import dto.resp.AdminLoginRespDTO;
import dto.resp.AdminQueryWaitViewArticleRespDTO;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;


public class AdminApp {
    private String name;
    private String username;
    private boolean isLogin;

    private AdminController adminController;
    private Scanner scanner;

    public AdminApp() {
        scanner = new Scanner(System.in);
        adminController = new AdminController();
        isLogin = false;
    }

    public void run() {
        int i = 0;
        do {
            LoginMenu();
            i = scanner.nextInt();
            switch (i){
                case 1:
                {
                    login();
                    break;
                }
                case 2:
                {
                    register();
                }
                case 3:
                    break;
                default:
                    System.out.println("输入错误请重新输入");
            }

        }while(i != 3);
    }

    public void LoginMenu(){
        System.out.println("-----------管理员系统-------");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.返回上一层");
        System.out.println("--------------------------");
    }


    public void login(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的登录名(大于6个字符)： ");
        String username = sc.next();
        if(username.length() < 6){
            System.out.println("登录名长度小于6个字符");
            return;
        }
        System.out.print("请输入你的登录密码(大于6个字符)： ");
        String password = sc.next();
        if(password.length() < 6){
            System.out.println("密码长度小于6个字符");
            return;
        }
        //调用Controller登录
        Results<AdminLoginRespDTO> result = adminController.login(username, password);
        if(!result.isSuccess()){
            System.out.println(result.getMessage());
            return;
        }
        //登录成功
        AdminLoginRespDTO adminLoginRespDTO = result.getData();
        this.name = adminLoginRespDTO.getName();
        this.isLogin = true;
        this.username = username;
        System.out.println(result.getMessage());

        //进入功能菜单
        fun();
    }

    public void fun(){
        if(!isLogin){
            System.out.println("登录已失效,请重新登录");
            return;
        }
        //1.审核功能
        //2.删除文章功能
        int i = 0;
        do {
            System.out.println("-----------------------------------");
            System.out.println("-----功能菜单-----管理员用户: " + username);
            System.out.println("1.审核文章");
            System.out.println("2.删除文章");
            System.out.println("3.查看待审核文章");
            System.out.println("4.退出");
            System.out.println("-----------------------------------");
            i  = this.scanner.nextInt();
            switch (i){
                case 1:
                    viewArticle();
                    break;
                case 2:
                    deleteArticle();
                    break;
                case 3:
                    queryArticle();
                    break;
                case 4:
                    break;
                default:
                    System.out.println("输入无效");
                    break;
            }
        }while(i != 4);
    }

    public void queryArticle(){
        //查看页
        int idx = 1;
        int i = 0;
        do {
            Results<ArrayList<AdminQueryWaitViewArticleRespDTO>> arrayListResults = adminController.queryWaitViewArticleAll(idx);
            if(!arrayListResults.isSuccess()){
                System.out.println(arrayListResults.getMessage());
            }
            ArrayList<AdminQueryWaitViewArticleRespDTO> articles = arrayListResults.getData();
            if(articles != null) {
                System.out.println("ID\t标题\t\t审核状态");
                for (AdminQueryWaitViewArticleRespDTO article : articles) {
                    System.out.println(article.getId() + "\t" + article.getTitle() + "\t\t" + (article.getIsView() == 0 ? "待审核" : "已审核"));
                }
            }
            System.out.println("第 " + idx + " 页： " + "1.上一页     2.下一页     3.退出");
            if(articles == null) {
                idx--;
            }
            i = this.scanner.nextInt();
            switch (i){
                case 1:
                    if(idx > 1)
                        idx--;
                    break;
                case 2:
                    idx++;
                    break;
                case 3:
                    break;
                default:
            }
        }while(i != 3);
    }


    public void deleteArticle(){
        System.out.print("输入要删除的文章编号: ");
        int id = scanner.nextInt();
        Results results = adminController.deleteArtile(id);
        if(!results.isSuccess()){
            //删除失败
            System.out.println(results.getMessage());
            return;
        }
        System.out.println(results.getMessage());
    }

    public void viewArticle(){
        System.out.print("输入审核通过的文章编号: ");
        int id = scanner.nextInt();
        Results results = adminController.viewArticle(id);
        if(!results.isSuccess()){
            //删除失败
            System.out.println(results.getMessage());
            return;
        }
        System.out.println(results.getMessage());
    }


    public void register(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的登录名(大于6个字符)： ");
        String username = sc.next();
        if(username.length() < 6){
            System.out.println("登录名长度小于6个字符");
            return;
        }
        System.out.print("请输入你的登录密码(大于6个字符)： ");
        String password = sc.next();
        if(password.length() < 6){
            System.out.println("密码长度小于6个字符");
            return;
        }
        System.out.print("请输入你的昵称(不能为空): ");
        String name = sc.next();
        if(name.length() < 1){
            System.out.println("昵称不能为空");
            return;
        }
        //调用Controller注册
        AdminRegisterReqDTO adminRegisterReqDTO = new AdminRegisterReqDTO();
        adminRegisterReqDTO.setName(name);
        adminRegisterReqDTO.setUsername(username);
        adminRegisterReqDTO.setPassword(password);
        Results results = adminController.register(adminRegisterReqDTO);
        //注册失败
        if(!results.isSuccess()){
            System.out.println(results.getMessage());
            return;
        }

        //注册成功
        System.out.println(results.getMessage());
    }
}
